kotlin data class 和 class 区别

kotlin data class 和 java class 区别

使用限制

data class 必须要有带参数的构造方法 (Data class must have at least one primary constructor parameter)

data class 不能被继承(Modifier ‘data’ is incompatible with ‘open’)

实现区别

普通class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Nearby(val age: Int)

//转成java

public final class Nearby {
private final int age;

public final int getAge() {
return this.age;
}

public Nearby(int age) {
this.age = age;
}
}

data class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

data class Nearby(val age: Int)

//转成java

public final class Nearby {
private final int age;

public final int getAge() {
return this.age;
}

public Nearby(int age) {
this.age = age;
}

public final int component1() {
return this.age;
}

@NotNull
public final Nearby copy(int age) {
return new Nearby(age);
}

// $FF: synthetic method
public static Nearby copy$default(Nearby var0, int var1, int var2, Object var3) {
if ((var2 & 1) != 0) {
var1 = var0.age;
}

return var0.copy(var1);
}

@NotNull
public String toString() {
return "Nearby(age=" + this.age + ")";
}

public int hashCode() {
return Integer.hashCode(this.age);
}

public boolean equals(@Nullable Object var1) {
if (this != var1) {
if (var1 instanceof Nearby) {
Nearby var2 = (Nearby)var1;
if (this.age == var2.age) {
return true;
}
}

return false;
} else {
return true;
}
}
}

两相对比,data class比class 多实现了 toString()、hashCode()、equals()、copy()、componentN()方法。
hashCode()、equals()是用来比较对象内容是否相同,多用于HashMap等容器中;
toString()是用来打印对象内容;
copy()实现了复制功能;
componentN()提供了快速访问元素的功能。
从上面看data class的功能 class都能实现,data class只是是kotlin提供的具有常用数据model功能的类,用于提升开发效率。

一点拓展

data class 可以有普通变量吗?答案是可以,那普通变量和元素变量有何区别呢?看下面的例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

data class Nearby(val age: Int) {
var name = "nearby"
}

///转成java

public final class Nearby {
@NotNull
private String name;
private final int age;

@NotNull
public final String getName() {
return this.name;
}

public final void setName(@NotNull String var1) {
Intrinsics.checkNotNullParameter(var1, "<set-?>");
this.name = var1;
}

public final int getAge() {
return this.age;
}

public Nearby(int age) {
this.age = age;
this.name = "nearby";
}

public final int component1() {
return this.age;
}

@NotNull
public final Nearby copy(int age) {
return new Nearby(age);
}

// $FF: synthetic method
public static Nearby copy$default(Nearby var0, int var1, int var2, Object var3) {
if ((var2 & 1) != 0) {
var1 = var0.age;
}

return var0.copy(var1);
}

@NotNull
public String toString() {
return "Nearby(age=" + this.age + ")";
}

public int hashCode() {
return Integer.hashCode(this.age);
}

public boolean equals(@Nullable Object var1) {
if (this != var1) {
if (var1 instanceof Nearby) {
Nearby var2 = (Nearby)var1;
if (this.age == var2.age) {
return true;
}
}

return false;
} else {
return true;
}
}
}